package assignment.ton.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import assignment.ton.def.Action;
import assignment.ton.def.Util;
import assignment.ton.def.Variable;
import assignment.ton.def.Class;
/**
 * This is a self listening class called when a constructor parameter needs to be added
 * @author Tim
 */
public class AddVariable extends JFrame implements ActionListener{
	private static final long serialVersionUID = -449939724337778651L;
	private Class theClass;
	private VariableFrame varFrame;
	private JLabel name, type, initialValue;
	private JTextField nameField, initialField;
	private JComboBox typeBox;
	private JRadioButton oneButton, manyButton;
	private ButtonGroup group;
	private JButton submit, cancel;
	private MainFrame mainFrame;
	public AddVariable(Class cc, VariableFrame cF, MainFrame mF){
		theClass = cc;
		mainFrame = mF;
		varFrame = cF;
		this.setTitle("Add Parameter");
		this.addWindowListener(new WindowAdapter(){ //now make it do something more interesting
			public void windowClosing(WindowEvent e){
					if(mainFrame.getAction()==Action.ADDCONSTRUCTOR){
						mainFrame.setAction(mainFrame.getPrevious2Action());
					}
			}
		});
		/**
		 * This is just a forwarder for when enter is pressed in the text box
		 */
		AbstractAction submitAction = new AbstractAction("Submit"){
			private static final long serialVersionUID = 7257370266664900955L;
			public void actionPerformed(ActionEvent e){
				submit();
			}
		};
		this.setLayout(new GridBagLayout());
		this.setIconImage(new ImageIcon(getClass().getResource("ton_icon.gif")).getImage());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(2,2,2,2);
		this.setResizable(false);
		this.setLocationRelativeTo(varFrame);
		name = new JLabel("Name");
		type = new JLabel("Type");
		initialValue = new JLabel("Initial");
		nameField = new JTextField(20);
		nameField.setAction(submitAction);
		String[] typeArray = Util.getDataTypes();
		typeBox = new JComboBox(typeArray);
		c.gridx = 0;
		c.gridy = 0;
		this.add(name, c);
		c.gridx = 1;
		this.add(nameField, c);
		c.gridx = 0;
		c.gridy = 1;
		this.add(type, c);
		c.gridx = 1;
		this.add(typeBox,c);
		c.gridx = 0;
		c.gridy = 2;
		this.add(initialValue, c);
		c.gridx = 1;
		initialField = new JTextField(20);
		this.add(initialField, c);
		oneButton = new JRadioButton("One");
		oneButton.setSelected(true);
		oneButton.setActionCommand("One");
		manyButton = new JRadioButton("Many");
		manyButton.setActionCommand("Many");
		group = new ButtonGroup();
		group.add(oneButton);
		group.add(manyButton);
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		JPanel radioPanel = new JPanel();
		this.add(radioPanel, c);
		radioPanel.add(oneButton);
		radioPanel.add(manyButton);
		JPanel bottomPanel = new JPanel();
		c.gridx = 0;
		c.gridy = 4;
		c.fill = GridBagConstraints.NONE;
		this.add(bottomPanel, c);
		submit = new JButton("Submit");
		bottomPanel.add(submit);
		submit.addActionListener(this);
		cancel = new JButton("Cancel");
		bottomPanel.add(cancel);
		cancel.addActionListener(this);
		pack();
	}
	@Override
	/**
	 * Simple action performed calling some error checking
	 */
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();
		if(actionCommand.equals("Submit")){
			submit();
		}else if(actionCommand.equals("Cancel")){
			this.dispose();
		}
	}
	/**
	 * This function is called when submit button is pressed or the text box action listener reacts
	 * It checks to make sure variables start with lower case and have no spaces then creates and adds the parameter
	 */
	public void submit(){
		String name = nameField.getText();
		String theType = (String)typeBox.getSelectedItem();
		theType = theType.trim();
		String initial = initialField.getText();
		if(name.equals("")==true){ Util.showError(this, "Name cannot be blank"); return; } 
		name = name.substring(0,1).toLowerCase()+name.substring(1);
		if(name.equals(nameField.getText())==false){
			Util.showError(this, "Must start with lowercase");
			return;
		}
		name = name.trim().replaceAll(" +", "");
		if(name.equals(nameField.getText())==false){
			Util.showError(this, "Must not contain white spaces");
			return;
		}
		name = name.replaceAll("[^A-Za-z]", "");
		if(name.equals(nameField.getText())==false){
			Util.showError(this, "Name must only be alphanumeric");
			return;
		}
		if(initial!=null&&initial.equals("")==false){
			if(Util.checkType(theType,initial)==false){ 
				Util.showError(this, "Incorrect initial value for type specified");
				return;
			} 
			if(theType.equals("char")){
				initial=initial.substring(0,1);
			}
		}
		if(manyButton.isSelected()&&(initial.equals("")==false||initial!=null)){
			Util.showWarning(this, "Initial values will not be set when there is many");
		}
		if(mainFrame.getAction()==Action.VARIABLES){
			if(theClass.hasVariable(name)){
				Util.showError(this, "Variable with this name already exists");
				return;
			}
			Variable v = new Variable(name, theType, manyButton.isSelected(), initial);
			theClass.addVariable(v);
			varFrame.rebuildList();
			this.dispose();
		}
	}

}
